package com.hxkcloud.patient.activitys;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.XueyaDetailListAdapter;
import com.hxkcloud.patient.views.ListDividerItemDecoration;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by ray_lei on 2016/7/10.
 */
public class XueyaDetailActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private TextView dayTV;
    private TextView weekTV;
    private TextView monthTV;
    private LinearLayout avgLayout;

    private boolean hasAxes = true;
    private boolean hasAxesNames = true;
    private boolean hasLines = true;
    private boolean hasPoints = true;
    private ValueShape shape = ValueShape.CIRCLE;
    private boolean isFilled = false;
    private boolean hasLabels = false;
    private boolean isCubic = false;
    private boolean hasLabelForSelected = false;
    private boolean pointsHaveDifferentColor;


    private LineChartView second_chart;
    private LineChartData second_data;
    private int sNumberOfLines = 2;
    private int sMaxNumberOfLines = 4;
    private int sNumberOfPoints = 5;
    float[][] sRandomNumbersTab = new float[sMaxNumberOfLines][sNumberOfPoints];

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<String> stringList;
    private XueyaDetailListAdapter adapter;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_xueyadetail);
        stringList = new ArrayList<String>();
        for (int i = 0; i < 6; i++) {
            stringList.add("");
        }

        second_chart = (LineChartView) findViewById(R.id.xueyadetail_chart);
//        chart.setOnValueTouchListener(new ValueTouchListener());

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getText(R.string.montitor_xueya_text));


        //chart2
        generateValues2();
        generateData2();
        second_chart.setViewportCalculationEnabled(false);
        resetViewport2();

        initView();
        initData();
        initListener();
    }


    private void generateValues2() {
        for (int i = 0; i < sMaxNumberOfLines; ++i) {
            for (int j = 0; j < sNumberOfPoints; ++j) {
                sRandomNumbersTab[i][j] = (float) Math.random() * 10f;
            }
        }
    }


    private void generateData2() {

        List<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < sNumberOfLines; ++i) {
            if (i == 0) {
                List<PointValue> values = new ArrayList<PointValue>();
                values.add(new PointValue(0, 65));
                values.add(new PointValue(1, 88));
                PointValue point = new PointValue(2, 95);
                point.setBig(true);
                values.add(point);
                values.add(new PointValue(3, 80));

                Line line = new Line(values);
                line.setColor(ChartUtils.COLORS[i]);
                line.setShape(shape);
                line.setCubic(isCubic);
                line.setFilled(isFilled);
                line.setHasLabels(hasLabels);
                line.setHasLabelsOnlyForSelected(hasLabelForSelected);
                line.setHasLines(hasLines);
                line.setHasPoints(hasPoints);
                if (pointsHaveDifferentColor) {
                    line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
                }
                lines.add(line);
            } else if (i == 1) {

                List<PointValue> values = new ArrayList<PointValue>();
                values.add(new PointValue(0, 140));
                values.add(new PointValue(1, 130));
                PointValue point = new PointValue(2, 160);
                point.setBig(true);
                values.add(point);
                values.add(new PointValue(3, 135));

                Line line = new Line(values);
                line.setColor(ChartUtils.COLORS[i]);
                line.setShape(shape);
                line.setCubic(isCubic);
                line.setFilled(isFilled);
                line.setHasLabels(hasLabels);
                line.setHasLabelsOnlyForSelected(hasLabelForSelected);
                line.setHasLines(hasLines);
                line.setHasPoints(hasPoints);
                if (pointsHaveDifferentColor) {
                    line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
                }
                lines.add(line);
            }

        }


        second_data = new LineChartData(lines);

        if (hasAxes) {
            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            mAxisValues.add(new AxisValue(0).setLabel("06:00")); //为每个对应的i设置相应的label(显示在X轴)
            mAxisValues.add(new AxisValue(1).setLabel("12:00"));
            mAxisValues.add(new AxisValue(2).setLabel("18:00"));
            mAxisValues.add(new AxisValue(3).setLabel("24:00"));

            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            yAxisValues.add(new AxisValue(60).setLabel("60")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(90).setLabel("90"));
            yAxisValues.add(new AxisValue(120).setLabel("120"));
            yAxisValues.add(new AxisValue(150).setLabel("150"));
            yAxisValues.add(new AxisValue(180).setLabel("180"));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);
            if (hasAxesNames) {
//                    axisX.setName("Axis X");
//                    axisY.setName("Axis Y");
            }
            second_data.setAxisXBottom(axisX);
            second_data.setAxisYLeft(axisY);
        } else {
            second_data.setAxisXBottom(null);
            second_data.setAxisYLeft(null);
        }

//            data.setBaseValue(Float.NEGATIVE_INFINITY);
        second_chart.setLineChartData(second_data);

    }

    private void resetViewport2() {
        // Reset viewport height range to (0,100)
        final Viewport v = new Viewport(second_chart.getMaximumViewport());
        v.bottom = 0;
        v.top = 180;
        v.left = 0;
        v.right = sNumberOfPoints - 1;
        second_chart.setMaximumViewport(v);
        second_chart.setCurrentViewport(v);
    }


    @Override
    protected void initView() {
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.xueyadetail_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.xueyadetail_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        ListDividerItemDecoration itemDecoration = new ListDividerItemDecoration(
                this, ListDividerItemDecoration.VERTICAL_LIST);
        recycleView.addItemDecoration(itemDecoration);

        adapter = new XueyaDetailListAdapter(this, stringList, recycleView);
        recycleView.setAdapter(adapter);

        dayTV = (TextView) findViewById(R.id.xueyadetail_tv_day);
        weekTV = (TextView) findViewById(R.id.xueyadetail_tv_week);
        monthTV = (TextView) findViewById(R.id.xueyadetail_tv_month);
        avgLayout = (LinearLayout) findViewById(R.id.xueyadetail_layout_avg);
    }

    @Override
    protected void initListener() {
        dayTV.setOnClickListener(this);
        weekTV.setOnClickListener(this);
        monthTV.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.xueyadetail_tv_day://血糖详情页

                dayTV.setTextColor(getResources().getColor(R.color.white));
                weekTV.setTextColor(getResources().getColor(R.color.light_blue));
                monthTV.setTextColor(getResources().getColor(R.color.light_blue));
                avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav));

                break;
            case R.id.xueyadetail_tv_week://血压详情页

                dayTV.setTextColor(getResources().getColor(R.color.light_blue));
                weekTV.setTextColor(getResources().getColor(R.color.white));
                monthTV.setTextColor(getResources().getColor(R.color.light_blue));
                avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav_two));

                break;
            case R.id.xueyadetail_tv_month:

                dayTV.setTextColor(getResources().getColor(R.color.light_blue));
                weekTV.setTextColor(getResources().getColor(R.color.light_blue));
                monthTV.setTextColor(getResources().getColor(R.color.white));

                avgLayout.setBackground(getResources().getDrawable(R.mipmap.blood_nav_three));

                break;

        }

    }
}
